using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Collections.Specialized;
using System.IO;
using System.Xml.Xsl;
using System.Text.RegularExpressions;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;
using LB.Generics;

using ExtraClub.Domain.Entity;
using ExtraClub.Domain.Framework;

namespace ExtraClub.Domain {
    public class IndexedProperty<Index, Return> {
        private Func<Index, Return> m_getter;
        public IndexedProperty(Func<Index, Return> getter) {
            if (getter == null) { throw new ArgumentNullException("getter", "getter is null."); }
            m_getter = getter;
        }

        public Return this[Index index] {
            get { return m_getter(index); }
        }
    }

    public partial class BaseUserControl {
        private Dictionary<string, string> _uiText = null;
        private Dictionary<string, string> _uiStyle = null;

        private Dictionary<string, List<string>> _uiSelectOptions = null;
        private Dictionary<string, List<string>> _uiSelectValues = null;

        public IndexedProperty<string, String> UIText { get; set; }
        public IndexedProperty<string, String> UIStyle { get; set; }

        public IndexedProperty<string, List<string>> UISelectOptions { get; set; }
        public IndexedProperty<string, List<string>> UISelectValues { get; set; }

        protected string GetUITextProperty(string key) {
            _uiText = GetUIText();
            if (_uiText.ContainsKey(key)) {
                return _uiText[key];
            }
            return "undefied";
        }

        protected string GetUIStyleProperty(string key) {
            _uiStyle = GetUIStyle();
            if (_uiStyle.ContainsKey(key)) {
                return _uiStyle[key];
            }
            return "undefied";
        }

        protected List<string> GetUISelectOptionsProperty(string key) {
            _uiSelectOptions = GetUISelectOptions();

            if (_uiSelectOptions.ContainsKey(key)) {
                return _uiSelectOptions[key];
            }else{
                return new List<string>(new string[]{"list not found"});
            }
        }
        protected List<string> GetUISelectValuesProperty(string key) {
            _uiSelectOptions = GetUISelectValues();
            if (_uiSelectOptions.ContainsKey(key)) {
                return _uiSelectOptions[key];
            } else {
                return new List<string>(new string[] { "list not found" });
            }
        }

        public Dictionary<string, string> GetUIText() {
            string path = Server.MapPath(TemplateControl.AppRelativeVirtualPath.Replace("module.ascx", "") + "/text.xml");
            if (File.Exists(path)) {
                string lang = SiteContext.Current.Language;
                string cacheKey = path + lang;
                if (_uiText == null) {
                    _uiText = (Dictionary<string, string>)HttpRuntime.Cache[cacheKey];
                    if (_uiText == null) {
                        _uiText = UIXmlManager.GetUITextCollection(path, lang);
                        WebUtils.PutIntoCach(cacheKey, path, _uiText);
                    }
                }
            }
            return _uiText;
        }

        public Dictionary<string, string> GetUIStyle() {
            string path = Server.MapPath(TemplateControl.AppRelativeVirtualPath.Replace("module.ascx", "") + "/style.xml");
            if (File.Exists(path)) {
                if (_uiStyle == null) {
                    _uiStyle = (Dictionary<string, string>)HttpRuntime.Cache[path];
                    if (_uiStyle == null) {
                        _uiStyle = UIXmlManager.GetUIStyleCollection(path);
                        WebUtils.PutIntoCach(path, path, _uiStyle);
                    }
                }
            }
            return _uiStyle;
        }

        #region Form Dropdawn Lists
        public Dictionary<string, List<string>> GetUISelectOptions() {
            string path = Server.MapPath(TemplateControl.AppRelativeVirtualPath.Replace("module.ascx", "") + "/text.xml");
            if (File.Exists(path)) {
                string lang = SiteContext.Current.Language;
                string cacheKey = path + lang + "select-options";
                if (_uiSelectOptions == null) {
                    _uiSelectOptions = (Dictionary<string, List<string>>)HttpRuntime.Cache[cacheKey];
                    if (_uiSelectOptions == null) {
                        _uiSelectOptions = UIXmlManager.GetSelectOptions(path, lang);
                        WebUtils.PutIntoCach(cacheKey, path, _uiSelectOptions);
                    }
                }
            }
            return _uiSelectOptions;
        }

        public Dictionary<string, List<string>> GetUISelectValues() {
            string path = Server.MapPath(TemplateControl.AppRelativeVirtualPath.Replace("module.ascx", "") + "/text.xml");
            if (File.Exists(path)) {
                string lang = SiteContext.Current.Language;
                string cacheKey = path + lang + "select-values";
                if (_uiSelectValues == null) {
                    _uiSelectValues = (Dictionary<string, List<string>>)HttpRuntime.Cache[cacheKey];
                    if (_uiSelectValues == null) {
                        _uiSelectValues = UIXmlManager.GetSelectValues(path, lang);
                        WebUtils.PutIntoCach(cacheKey, path, _uiSelectValues);
                    }
                }
            }
            return _uiSelectValues;
        }
        #endregion
    }
}
